﻿using System;
using System.Collections.Generic;
using System.Linq;

using BEX.DataAccess;
using BEX.Core;
using BEX.Core.Exchange;

namespace BEX.RuleMonitor
{
    public abstract class AlertRule
    {
        protected int _id;

        public int ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        protected int _sourceExchangeID;

        public int SourceExchangeID
        {
            get
            {
                return _sourceExchangeID;
            }
            set
            {
                _sourceExchangeID = value;
            }
        }

        public ExchangeType SourceExchange
        {
            get
            {
                return (ExchangeType)(_sourceExchangeID);
            }

            set
            {
                _sourceExchangeID = (int)((ExchangeType)value);
            }
        }

        protected bool _isActive;

        protected CurrencyPair _pair;

        public CurrencyPair Pair
        {
            get { return _pair; }
            set { _pair = value; }
        }

        /*

        protected int _baseCurrencyID;

        public int BaseCurrencyID
        {
            get
            {
                return _baseCurrencyID;
            }
            set
            {
                _baseCurrencyID = value;
            }
        }

        public CurrencyType BaseCurrency
        {
            get
            {
                return (CurrencyType)(_baseCurrencyID);
            }
            set
            {
                _baseCurrencyID = (int)(CurrencyType)value;
            }
        }

        protected int _counterCurrencyID;

        public int CounterCurrencyID
        {
            get
            {
                return _counterCurrencyID;
            }
            set
            {
                _counterCurrencyID = value;
            }
        }

        public CurrencyType CounterCurrency
        {
            get
            {
                return (CurrencyType)(_counterCurrencyID);
            }
            set
            {
                _counterCurrencyID = (int)((CurrencyType)value);
            }
        }
        */

        public AlertRule()
        {
        }

        public abstract bool Test(Tick current);

        public abstract void AddAccount(int AccID);

        public abstract void RemoveAccount(int AccID);

        public abstract List<int> GetAccountIDList();

        public double GetCoolDownTime(int AccID)
        {
            var _context = new BTCDataClassesDataContext();
            

            switch (this.GetType().ToString())
            {
                case ("BEX.RuleMonitor.PercentageRule"):
                    var _percentRule = (from c in _context.GetTable<ACCOUNT_RULE_PERCENTAGE>()
                                              where (c.RULE_ID == this.ID && c.ACCOUNT_ID == AccID)
                                              select c).Single();

                    if (null != _percentRule)
                    {
                        return Convert.ToDouble(_percentRule.COOLDOWN_TIME);
                    }
                    break;
                case ("BEX.RuleMonitor.FlatAmountRule"):
                    var _flatRule = (from c in _context.GetTable<ACCOUNT_RULE_FLAT>()
                                              where (c.RULE_ID == this.ID && c.ACCOUNT_ID == AccID)
                                              select c).Single();

                    if (null != _flatRule)
                    {
                        return Convert.ToDouble(_flatRule.COOLDOWN_TIME);
                    }
                    break;
                case ("BEX.RuleMonitor.IncrementRule"):
                    var _incrementRule = (from c in _context.GetTable<ACCOUNT_RULE_INCREMENT>()
                                              where (c.RULE_ID == this.ID && c.ACCOUNT_ID == AccID)
                                              select c).Single();

                    if (null != _incrementRule)
                    {
                        return Convert.ToDouble(_incrementRule.COOLDOWN_TIME);
                    }
                    break;
                case ("BEX.RuleMonitor.ArbitrageRule"):
                    var _arbitrageRule = (from c in _context.GetTable<ACCOUNT_RULE_ARBITRAGE>()
                                              where (c.RULE_ID == this.ID && c.ACCOUNT_ID == AccID)
                                              select c).Single();

                    if (null != _arbitrageRule)
                    {
                        return Convert.ToDouble(_arbitrageRule.COOLDOWN_TIME);
                    }
                    break;
                default:

                    break;
            }

            return -1;
        }

        public DateTime LastNofiticationTime(int AccID)
        {
            var _context = new BTCDataClassesDataContext();


            var _n = (from c in _context.GetTable<NOTIFICATION_QUEUE>()
                                where (c.RULE_ID == this.ID && c.RULE_TYPE == this.GetType().ToString() && c.ACCOUNT_ID == AccID)
                                orderby c.ID descending
                                select c).Take(1);

            if (null != _n && _n.ToList().Count > 0)
            {
                return _n.ToList()[0].TIMESTAMP;
            }
            else
            {
                return DateTime.MinValue;
            }
        }
    }
}
